//2009/08/26 22:15:23
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class SnowyWinter
{
public:
    int snowyHighwayLength(vector <int> startPoints, vector <int> endPoints)
    {
		vector<pair<int, int> > v;
		for(int i=0; i<endPoints.size(); i++)
			v.push_back(make_pair(startPoints[i], endPoints[i]));
		sort(v.begin(), v.end());
		int total = 0;
		int last = 0;
		for(int i=0; i<v.size(); i++)
		{
			total += max(v[i].second - max(last, v[i].first), 0);
			last = max(last, v[i].second);
		}
		return total;
    }
};
